package com.buss.product.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.buss.core.persistence.OrderBy;
import com.buss.core.persistence.Paginalbe;
import com.buss.core.persistence.ResultList;
import com.buss.core.utils.StringUtil;
import com.buss.product.dao.BrandDao;
import com.buss.product.model.Brand;
import com.buss.product.model.GoodsCategory;
import com.buss.product.service.BrandService;
import com.buss.product.service.GoodsCategoryService;

@Service
public class BrandServiceImpl implements BrandService {

	@Autowired
	private BrandDao brandDao;
	
	@Autowired
	private GoodsCategoryService categoryService;
	
	@Override
	public Brand saveBrand(Brand brand) {
		return brandDao.saveBrand(brand);
	}

	@Override
	public void editBrand(Brand brand) {
		brandDao.editBrand(brand);
	}

	@Override
	public void removeBrand(Long[] ids) {
		brandDao.removeBrand(ids);
	}

	@Override
	public Brand loadBrand(Long id) {
		return brandDao.loadBrand(id);
	}

	@Override
	public ResultList<Brand> queryBrand(String companyId,String branchId, String name,String pinyin,
			Long categoryId, OrderBy orderBy, Paginalbe page) {
		Long[] categoryIds = null;
		if(categoryId != null){
			List<GoodsCategory> list = categoryService.queryCategoryByParentId(companyId, branchId, categoryId);
			categoryIds = new Long[list.size() + 1];
			categoryIds[0] = categoryId;
			for(int i=0 ; i < list.size();i++){
				categoryIds[i + 1] = list.get(i).getId();
			}
		}
		
		return brandDao.queryBrand(companyId, name,pinyin, categoryIds, orderBy, page);
	}

	/**
	 * 根据商品分类编码获取对应的品牌信息
	 * @param categoryId
	 * @return
	 */
	public ResultList<Brand> queryBrand(Long categoryId){
		return brandDao.queryBrand(categoryId);
	}
	
	/**
	 * 获取指定分类编码没有关联的品牌信息
	 * @param categoryId
	 * @return
	 */
	public ResultList<Brand> queryBrandNotInCategoryId(Long categoryId){
		return brandDao.queryBrandNotInCategoryId(categoryId);
	}
	
}
